//
//  HomeChoiceCityVc.m
//  QuickCure1
//
//  Created by Ashzenki on 02/11/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "HomeChoiceCityVc.h"
#import "HomeViewController.h"
#import "HomeProvinceModel.h"
#import "WeatherView.h"

@interface HomeChoiceCityVc ()<UINavigationControllerDelegate, UIViewControllerAnimatedTransitioning>

@property (nonatomic, strong) UITableViewController *fromVc;

@end

@implementation HomeChoiceCityVc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 50;
    
    
    [self setUpLeftButtonItem];
    
    self.navigationItem.title = @"选择城市";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor],
                                                                    NSFontAttributeName:MYFONT(18)};
    
    self.navigationController.delegate = self;
   
}

- (void)setUpLeftButtonItem{
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}

- (void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _CityList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NSString *city = _CityList[indexPath.row];
    
    cell.textLabel.text = city;
    cell.textLabel.font = MYFONT(18);
    
    //设置分割线
    UIImageView *separatView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"table_item_bk_with_top_gray_corner"]];
    [cell.contentView addSubview:separatView];
    
    [separatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(cell.contentView);
        make.height.mas_equalTo(5);
    }];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"oo");
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    HomeViewController *homeVc = self.navigationController.viewControllers[0];
    
    //将选中的城市传给主页
    [homeVc.weatherView.bt_location setTitle:cell.textLabel.text forState:UIControlStateNormal];
    
    [self.navigationController popToViewController:homeVc animated:YES];
    
}



///-----------------------------------------------------------------------------转场动画

#pragma mark - 转场动画代理方法

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC  {
    
    return self;
}

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    
    return 1;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *containerView = [transitionContext containerView];
    
    [containerView addSubview:toVc.view];
    
    //自定义转场动画
    UIView *view = toVc.view;
    CATransition *anima = [CATransition animation];
    anima.type =  @"suckEffect";
    anima.subtype = kCATransitionFromRight;
    anima.duration = 1.0;
    [view.layer addAnimation:anima forKey:@"suckEffectAnimation"];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        toVc.view.alpha = 1;
        
    } completion:^(BOOL finished) {
        NSLog(@"111");
        
        [transitionContext completeTransition:YES];

    }];
    
}

- (void)dealloc {
    
    NSLog(@"选择城市释放");
}

@end



































